<?php

declare (strict_types=1);

namespace App\Application\Admin\Model;

use App\Application\Admin\Service\AccessService;
use App\Application\Admin\Model\Json;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $room_id
 * @property string $name
 * @property int $store_id
 * @property int $status
 * @property string $image_url
 * @property array $label
 * @property array $package_id
 * @property int $door_type
 * @property string $door_id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \Hyperf\Database\Model\Collection|self[] $children
 */
class Room extends Model
{
    use SoftDeletes;

    protected string $primaryKey = 'room_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'room';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = ['room_id','name', 'image_url', 'status', 'label', 'package_id', 'door_type', 'door_id', 'admin_id', 'store_id','sort'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'room_id' => 'integer',
        'name' => 'string',
        'package_id' =>Json::class,
        'label' =>Json::class,
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * 获取用户的姓名.
     *
     * @param  string  $value
     * @return string
     */
    public function getFirstPackageIdAttribute($value)
    {
        return ucfirst($value);
    }

    /**
     * 创建门店
     *
     * @param string $name
     * @param string $password
     * @param string $real_name
     * @param int $role_id
     * @return bool
     * @throws ErrorException
     */
    static function createStore(int $room_id, array $room = []): self
    {
        Db::beginTransaction();
        $room = self::updateOrCreate(['room_id' => $room_id], $room);
        Db::commit();
        return $room;

    }

}